<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../lib/layui/css/layui.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
</head>

<body>
<div class="layui-fluid ">
    <div class="layui-row layui-card">
        <div class="site-text site-block">
            <div class="layui-form  layui-col-space5"  lay-filter="example">
                <div class="layui-form-item"></div>
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>套餐名称</label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" placeholder="" required="" lay-verify="required" autocomplete="off" class="layui-input">
                        <input type="hidden" id="id" name="id" />
                    </div>

                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>基本工资</label>
                    <div class="layui-input-inline">
                        <input type="text" id="basicsalary" name="basicsalary" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>交通补助</label>
                    <div class="layui-input-inline">
                        <input type="text" id="trafficsalary" name="trafficsalary" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>

                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>午餐补助</label>
                    <div class="layui-input-inline">
                        <input type="text" id="lunchsalary" name="lunchsalary" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>

                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>奖金</label>
                    <div class="layui-input-inline">
                        <input type="text" id="bonus" name="bonus" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                    <span class="x-red">*</span>养老金比率</label>
                    <div class="layui-input-inline">
                        <input type="text" id="pensionper" name="pensionper" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>

                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>养老金基数</label>
                    <div class="layui-input-inline">
                        <input type="text" id="pensionbase" name="pensionbase" placeholder="" required="" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>医疗保险比率</label>
                    <div class="layui-input-inline">
                        <input type="text" id="medicalper" name="medicalper" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>

                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>医疗保险基数</label>
                    <div class="layui-input-inline">
                        <input type="text" id="medicalbase" name="medicalbase" placeholder="" required="" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>公积金比率</label>
                    <div class="layui-input-inline">
                        <input type="text" id="accumulationfundper" name="accumulationfundper" placeholder="" required="" lay-verify="required|validateMoney" autocomplete="off" class="layui-input">
                    </div>

                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>公积金基数</label>
                    <div class="layui-input-inline">
                        <input type="text" id="accumulationfundbase" name="accumulationfundbase" placeholder="" required="" lay-verify="required" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="add">保存工资套账</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<script>

function getQueryVariable(variable){
    let query = window.location.search.substring(1)
    let vars = query.split("&")
    for(let i=0; i< vars.length; i++){
        let pair = vars[i].split("=")
        if( pair[0] == variable){
            return pair[1]
        }
    }
    return false
}

layui.use(['form' , 'layer'] , function(){
    let form = layui.form
    let id = getQueryVariable("id")

    $.ajax({
        type: 'GET',
        url: '/salary/id/'+ id,
        dataType: 'json',
        contentType: 'application/json;charset=utf-8',
        success: function (object) {
            let salary = object.data
            form.val('example' ,{
                "id": salary.id ,
                "name": salary.name,
                "basicsalary": salary.basicsalary ,
                "bonus": salary.bonus,
                "lunchsalary": salary.lunchsalary,
                "trafficsalary": salary.trafficsalary,
                "allsalary": salary.allsalary,
                "pensionbase": salary.pensionbase,
                "pensionper": salary.pensionper,
                "createdate": salary.createdate,
                "medicalbase": salary.medicalbase,
                "medicalper": salary.medicalper,
                "accumulationfundbase": salary.accumulationfundbase,
                "accumulationfundper": salary.accumulationfundper
            })


        } , error: function(xhr){
            let data = JSON.parse( xhr.responseText)
            layer.msg( data.message )
        }
    })

    form.verify({
        validateMoney: function(value){
            let result = validateMoney( value)
            if( result != 'Y') {
                return result
            }
        }
    })

    function validateMoney(money) {
        let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|( ^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/
        if( reg.test(money)) {
            return 'Y'
        }
    }

    form.on('submit(add)' , function(data){
        $.ajax({
            type: 'PUT',
            url: '/salary',
            data: JSON.stringify(data.field),
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function (object) {

                layer.alert( object.message , function(){
                    let index = parent.layer.getFrameIndex(window.name)
                    parent.layer.close( index)
                })

            }, error : function(xhr){
                let data = JSON.parse( xhr.responseText)
                layer.msg( data.message )
            }
        })
    })

})

</script>
</body>

</html>